let express = require('express');
let router = express.Router();
let svgCaptcha = require('svg-captcha');
/**
 * 获取验证码
 * @route GET /api/public/v1/captcha
 * @group 登录 - login
 * @returns {object} 成功 -验证码
 * @returns {ResponseMeta.model} 错误异常
 */
router.get("/", (req, res, next) => {
    try {
        //验证码配置api
        let options = {
            //线条数
            noise: Math.floor(Math.random() * 5) + 1,
            color: true,
            width: 120,
            height: 45,
            background: '#dcdfe6',
        }, captcha;
        if (Math.floor(Math.random() * 3) > 1) {
            captcha = svgCaptcha.createMathExpr(options)
        } else {
            captcha = svgCaptcha.create(options);
        }
        let resData = {
            codeSvg: captcha.data,
            key: new Date().getTime()
        }
        req.session.captcha_text = captcha.text.toLowerCase();
        req.session.captcha_key = resData.key;
        // 10m后清除验证码
        setTimeout(() => {
            delete req.session.captcha_text;
            delete req.session.captcha_key;
        }, 10 * 60 * 1000);
        res.sendResult({ data: resData, code: 0, message: "获取验证码成功" });
    } catch (err) {
        next(err)
    }
})

/**
 * 获取短信验证码
 * @route GET /api/public/v1/captcha/sms
 * @group 登录 - login
 * @param {string} mobile.query.required - 手机号
 * @returns {ResponseMeta.model} 成功
 * @returns {ResponseMeta.model} 错误异常
 */
router.get("/sms", (req, res, next) => {
    try {
        // todo 自行实现发送短信验证码
        let mobile = req.query.mobile;
        let code = Math.floor(Math.random() * 900000) + 100000;
        res.sendResult({ data: code, code: 0, message: "获取短信验证码成功" });
    } catch (err) {
        next(err)
    }
})




module.exports = router;

